package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.AlipayBindStatusResponse;
import com.ysxsoft.wdg.bean.response.MyShouYiResponse;
import com.ysxsoft.wdg.bean.response.SubmitTiXianResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class TxActivity extends BaseActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, TxActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_zfb)
    TextView tvZfb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tx;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("我要提现");
        getStatus();
    }

    @Override
    protected void setListener() {

    }

    @OnClick({R.id.title_finish, R.id.tv_zfb, R.id.btn_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_zfb:
                BindZfbActivity.start(this, tvZfb.getText().toString().trim());
                break;
            case R.id.btn_sure:
                if ("".equals(etMoney.getText().toString())) {
                    ToastUtils.showToast("请输入提现金额！");
                    return;
                }
                if(!isStarted){
                    isStarted=true;
                    submitTiXian();
                }
                break;
        }
    }

    private boolean isStarted=false;

    /**
     * 获取我的账户
     */
    private void getStatus() {
        OkHttpUtils.post()
                .url(UrlHost.ALIPAY_STATUS)
                .addParams("uid", SharePrefUtils.getUserId())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    AlipayBindStatusResponse alipayBindStatusResponse = JsonUtils.parseByGson(response, AlipayBindStatusResponse.class);
                    if (alipayBindStatusResponse == null) {
                        return;
                    }
                    if (!CallbackCode.SUCCESS.equals(alipayBindStatusResponse.getCode())) {//未绑定支付宝账户
                        BindZfbActivity.start(TxActivity.this, tvZfb.getText().toString().trim());
                    } else {
                        tvZfb.setText(alipayBindStatusResponse.getData());
                    }
                } else {
                }
            }
        });
    }

    private String alicount;

    /**
     * 获取我的账户
     */
    private void submitTiXian() {
        OkHttpUtils.post()
                .url(UrlHost.TX_SUBMIT)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("account", etMoney.getText().toString())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                isStarted=false;

                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                isStarted=false;
                if (response != null) {
                    SubmitTiXianResponse submitTiXianResponse = JsonUtils.parseByGson(response, SubmitTiXianResponse.class);
                    if (submitTiXianResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(submitTiXianResponse.getCode())) {
                        ToastUtils.showToast(submitTiXianResponse.getMs());
                        finish();
                    }else{
                        ToastUtils.showToast(submitTiXianResponse.getMs());
                    }
                } else {
                }
            }
        });
    }


}
